package com.bsh.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.bsh.api.ApiUtils;
import com.bsh.api.Exception.ApiException;
import com.bsh.api.params.DtoBankInfo;
import com.bsh.mybatis.model.Bank;
import com.bsh.mybatis.model.User;
import com.bsh.mybatis.model.UserBank;
import com.bsh.service.ApiUserBankService;
import com.bsh.service.BankService;
import com.bsh.service.UserBankService;
import com.bsh.service.UserService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;
import com.bsh.utils.Des3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApiUserBankServiceImpl implements ApiUserBankService {
    @Autowired
    UserBankService ubs;
    @Autowired
    UserService servUser;
    @Autowired
    BankService bankService;

    @Override
    @Transactional
    public ApiMessage saveUserBank(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        Bank bank = bankService.selectByPrimaryKey(dto.getInteger("bankId"));
        if (ApiUtils.objIsEmpty(dto.getInteger("bankId"))) {
            throw new ApiException("105", "缺少银行编号");
        } else if (ApiUtils.objIsEmpty(bank)) {
            throw new ApiException("105", "银行卡不存在");
        }
        if (ApiUtils.objIsEmpty(dto.getString("bankCard"))) {
            throw new ApiException("105", "缺少银行卡号或企业银行账号");
        }
        if (ApiUtils.objIsEmpty(dto.getString("isMerchant"))) {
            throw new ApiException("105", "缺少个人或商户标记");
        } else if (!(dto.getString("isMerchant").equals("-1")) && !(dto.getString("isMerchant").equals("1"))) {
            throw new ApiException("105", "标记不存在");
        }
        if (ApiUtils.objIsEmpty(dto.getString("identifyName"))) {
            throw new ApiException("105", "缺少持卡人姓名或企业名称");
        } else if (!ApiUtils.isName(dto.getString("identifyName"))) {
            throw new ApiException("105", "持卡人姓名或企业名称非法");
        }
        // 查询出对应的用户对象
        User user = servUser.getUserByName(dto.getString("userName"));
        if (ApiUtils.objIsEmpty(user)) {
            throw new ApiException("102", "用户不存在");
        }
        // 查询是否已经添加过相同的银行卡
        List<Map<String, Object>> list = ubs.selectByBankCard(dto.getString("bankCard"));
        if (ApiUtils.objIsNotEmpty(list)) {
            throw new ApiException("106", "银行卡已存在");
        }
        // 查询用户的卡总数是否超过限制
        int num = ubs.countBankCards(user.getId(), dto.getInteger("isMerchant"));
        if (dto.getInteger("isMerchant") == -1 && num + 1 > Constants.BANKCARD.USERCARDS) {
            throw new ApiException("106", "个人银行卡数量超限(最多" + Constants.BANKCARD.USERCARDS + "张)");
        } else if (num + 1 > Constants.BANKCARD.MERCHANTCARDS) {
            throw new ApiException("106", "企业账户数量超限(最多" + Constants.BANKCARD.MERCHANTCARDS + "张)");
        }
        // 处理结果，添加数据
        UserBank ubReq = new UserBank();
        ubReq.setUserid(user.getId());
        ubReq.setBankid(dto.getInteger("bankId"));
        ubReq.setBankZone(dto.getString("bankZone"));
        ubReq.setBankCard(dto.getString("bankCard"));
        ubReq.setBankName(bank.getName());
        ubReq.setIdentifyName(dto.getString("identifyName"));
        ubReq.setIsMerchant(dto.getShort("isMerchant"));
        ubReq.setStatus((short) 1);
        ubReq.setCreateTime(new Date());
        int res = ubs.addUserBank(ubReq);
        if (res > 0) {
            String msgTitle = "添加用户银行成功!";
            msg = new ApiMessage("200", msgTitle);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("token", dto.getString("token"));
            msg.setData(Des3.encResponse(data));
        } else {
            throw new ApiException("400", "添加用户银行失败!");
        }
        return msg;
    }

    @Override
    public ApiMessage queryBankList(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        List<DtoBankInfo> list = bankService.queryBankList();
        msg = new ApiMessage("200", "可用的银行卡列表");
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("token", dto.getString("token"));
        data.put("allCardList", list);
        msg.setData(Des3.encResponse(data));
        return msg;
    }

    @Override
    public ApiMessage queryMyBankList(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        User user = servUser.getUserByName(dto.getString("userName"));
        if (ApiUtils.objIsEmpty(user)) {
            throw new ApiException("102", "用户不存在");
        }
        List<Map<String, Object>> list = ubs.getMyBankList(user.getId());
        String url = null;
        for (Map<String, Object> dmbi : list) {
            url = (String) dmbi.get("img");
            dmbi.remove("img");
            dmbi.put("img", dto.getString("serv_ip") + url);
        }
        msg = new ApiMessage("200", "我的银行卡列表");
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("token", dto.getString("token"));
        data.put("myCardList", list);
        msg.setData(Des3.encResponse(data));
        return msg;
    }

    @Override
    @Transactional
    public ApiMessage delUserBank(JSONObject dto) throws ApiException {
        ApiMessage msg = null;
        ApiUtils.checkUserNameToken(dto);
        if (ApiUtils.objIsEmpty(dto.getInteger("bankCardId"))) {
            throw new ApiException("103", "缺少银行卡id");
        }
        User user = servUser.getUserByName(dto.getString("userName"));
        if (ApiUtils.objIsEmpty(user)) {
            throw new ApiException("102", "用户不存在");
        }
        int res = ubs.deleteUserOneBankCard(dto.getInteger("bankCardId"), user.getId());
        if (res > 0) {
            msg = new ApiMessage("200", "删除成功");
        } else {
            msg = new ApiMessage("400", "删除失败");
        }
        return msg;
    }
}
